package org.streets.eis.ext.analysis.entities;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;
import org.streets.commons.util.StringUtils;
import org.streets.database.datadict.ColumnDefinition;
import org.streets.eis.ext.analysis.components.ColumnWrapper;


public class Field implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id
	private String id;
	@Column
	private String queryId;
	@Column
	private String tableCode;
	@Column
	private String tableName;
	@Column
	private String fieldCode;
	@Column
	private String fieldName;
	private String dataType;
	@Column
	private Integer showIndex;
	
	private String attr;
	
	public Field(){
		this.id="";
		this.queryId="";
		this.tableCode = "";
		this.tableName = "";
		this.fieldCode = "";
		this.fieldName = "";
		this.dataType  = "string";
		this.attr = "";

	}
	
	public Field(String tableCode,String fieldCode,String fieldName){
		this.tableCode = tableCode;
		this.fieldCode = fieldCode;
		this.fieldName = fieldName;
	}

	public Field(ColumnDefinition col){
		if (col != null){
			this.tableCode = col.getTable().getName();
			this.tableName = col.getTable().getAlias();
			this.fieldCode = col.getName();
			this.fieldName = col.getAlias();
			this.attr = col.getAttr();
			setDataType(col.getType().getSimpleName());
		}
	}
	
	public Field(ColumnWrapper col){
		if (col != null){
			this.tableCode = col.getTable().getName();
			this.tableName = col.getTable().getAlias();
			this.fieldCode = col.getName();
			this.fieldName = col.getAlias();
			this.attr = col.getAttr();
			setDataType(col.getType());
		}
	}
	
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	public String getFieldName() {
		return fieldName;
	}
	public void setFieldCode(String fieldCode) {
		this.fieldCode = fieldCode;
	}
	public String getFieldCode() {
		return fieldCode;
	}
	public void setTableCode(String tableCode) {
		this.tableCode = tableCode;
	}
	public String getTableCode() {
		return tableCode;
	}
	public void setQueryId(String queryId) {
		this.queryId = queryId;
	}
	public String getQueryId() {
		return queryId;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getId() {
		return id;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getTableName() {
		return tableName;
	}

	public void setShowIndex(Integer dispIndex) {
		this.showIndex = dispIndex;
	}

	public Integer getShowIndex() {
		return showIndex;
	}

	public void setDataType(String dataType) {
		if (isInteger(dataType)){
			this.dataType = "integer";
		} else if (isDouble(dataType)){
			this.dataType = "double";
		} else if (isDate(dataType)){
			this.dataType = "date";
		} else if (isTime(dataType)){
			this.dataType = "time"; 
		} else if (isDateTime(dataType)){
			this.dataType = "datetime";
		} 
		else{
			this.dataType = "string";
		}
	}
	
	private boolean isInteger(String dataType){
		String sint = "Short,short,Integer,integer,int,Long,long";
		return StringUtils.contains(sint, dataType);
	}
	
	private boolean isDouble(String dataType){
		String sdouble = "Float,float,Double,double,Numeric,numeric,Decimal,decimal,Real,real";
		return StringUtils.contains(sdouble, dataType);
	}
		
	private boolean isDate(String dataType){
		String sdate = "Date";
		return StringUtils.contains(sdate, dataType);
	}
	
	private boolean isTime(String dataType){
		String sdate = "Time";
		return StringUtils.contains(sdate, dataType);
	}

	private boolean isDateTime(String dataType){
		String sdate = "DateTime,timestamp";
		return StringUtils.contains(sdate, dataType);
	}
		
	public String getDataType() {
		return dataType;
	}
	
	public Boolean isCodeField() {
		if ((attr != null) && (attr.length() > 1)){
			return true;
		}
			
		return false;		
	}
	
	public Boolean isForeignKey() {
		if ((attr != null) && (attr.equalsIgnoreCase("F"))){
			return true;
		}
			
		return false;		
	}

	public void setAttr(String attr) {
		this.attr = attr;
	}
	public String getAttr() {
		return attr;
	}
	
}
